package com.二叉树2;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class 中序遍历 {
    static class Solution {




        public List<Integer> inorderTraversal(TreeNode root) {
                TreeNode cur  = root;
                List<Integer> list = new ArrayList<>();
                Stack<TreeNode> stack = new Stack<>();
                while (!stack.isEmpty() || cur != null){
                    while (cur != null){
                        stack.push(cur);
                        cur = cur.left;
                    }
                    cur = stack.pop();
                    list.add(cur.val);
                    cur = cur.right;
                }
                return list;

            }



        public List<Integer> inorderTraversal2(TreeNode root) {

            List < Integer > res = new ArrayList < > ();
            Stack < TreeNode > stack = new Stack < > ();
            TreeNode curr = root;
            while (curr != null || !stack.isEmpty()) {
                while (curr != null) {
                    stack.push(curr);
                    curr = curr.left;
                }
                curr = stack.pop();
                res.add(curr.val);
                curr = curr.right;
            }
            return res;
        }

        public List<Integer> inorderTraversal3(TreeNode root) {
            ArrayList<Integer> list = new ArrayList<>();
            if(root == null){
                return list;
            }
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = root;

            while (!stack.isEmpty() || cur != null){
               while (cur != null){
                   stack.push(cur);
                   cur = cur.left;
               }

               cur = stack.pop();
               list.add(cur.val);
               cur = cur.right;

            }
            return list;

        }
        public static void main(String[] args) {
            Solution solution = new Solution();
            List<Integer> list = solution.inorderTraversal3(new TreeNode(new int[]{1, 2, 3}));

        }

    }


}
